package com.lcb.jmm.aqs;

import java.util.concurrent.CountDownLatch;

/****
 * @Author:刘长柏
 * @Date: 2021-01-04 15:25
 * @Description: CountDownLaunch测试例子
 *****/
public class CountDownLaunchSample {
    public static void main(String[] args) throws InterruptedException {

        long now = System.currentTimeMillis();
        final CountDownLatch countDownLatch = new CountDownLatch(2);

        new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    if (countDownLatch != null) {
                        countDownLatch.countDown();
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    if (countDownLatch != null) {
                        countDownLatch.countDown();
                    }
                }
            }
        }).start();

        countDownLatch.await();

        System.out.println("完成");
        System.out.println("over，回家 cost:" + (System.currentTimeMillis() - now));
    }
}
